/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <math.h>
#include <windows.h>

#include "AudioBuffer.h"
#include "ClickGenerator.h"

ClickGenerator::ClickGenerator() throw(): m_bpm(0), m_sigBpm(0), m_sigBnt(0), m_beat(0), m_offset(0.0f) {
}

void ClickGenerator::reset(unsigned int bpm, unsigned int sigBpm, unsigned int sigBnt) throw() {
    m_bpm = bpm;
    m_sigBpm = sigBpm;
    m_sigBnt = sigBnt;

    m_beat = 0;
    m_offset = 0.0f;
}

void ClickGenerator::generateClicks(AudioBuffer *buffer) throw() {
    // Determine which functions to use.
    TimeToPtr timeToPtr;
    PtrToTime ptrToTime;
    PtrToNextSample ptrToNextSample;
    WriteSample writeSample;

    switch(buffer->numChannels()) {
        case 1:
            switch(buffer->bytesPerSample()) {
                case 1:
                    switch(buffer->samplesPerSecond()) {
                        case 11025:
                            timeToPtr = timeToPtr1M8;
                            ptrToTime = ptrToTime1M8;
                            ptrToNextSample = ptrToNextSample1M8;
                            writeSample = writeSample1M8;
                            break;
                        case 22050:
                            timeToPtr = timeToPtr2M8;
                            ptrToTime = ptrToTime2M8;
                            ptrToNextSample = ptrToNextSample2M8;
                            writeSample = writeSample2M8;
                            break;
                        case 44100:
                            break;
                        default:
                            ASSERT(!"Unsupported audio format!");
                            break;
                    }
                    break;
                case 2:
                    break;
                default:
                    ASSERT(!"Unsupported audio format!");
                    break;
            }
            break;
        case 2:
            break;
        default:
            ASSERT(!"Unsupported audio format!");
            break;
    }

    // Zero out the entire buffer.
    ZeroMemory(buffer->buffer(), buffer->bufferSize());

    // Now create the clicks.
    float dT = 60.0f / m_bpm;
    float time = m_offset;
    void *clickStart;
    const void *end = static_cast<char *>(buffer->buffer()) + buffer->bufferSize();
    for(clickStart = timeToPtr(buffer->buffer(), m_offset); clickStart < end; clickStart = timeToPtr(clickStart, dT)) {
        writeClickPulse(clickStart, end, timeToPtr, ptrToNextSample, writeSample, (m_beat % m_sigBpm) == 0);
        time += dT;
        m_beat += 1;
    }

    m_offset = ptrToTime(reinterpret_cast<void *>(static_cast<char *>(clickStart) - static_cast<const char *>(end)));
}

void ClickGenerator::writeClickPulse(void *start, const void *endOfBuffer, TimeToPtr timeToPtr, PtrToNextSample ptrToNextSample, WriteSample writeSample, bool acc) throw() {
    void *ptr = start;
    unsigned int durInSamples = reinterpret_cast<unsigned int>(timeToPtr(0, CLICKDURARTION));
    float dAngle = 3.14156f / durInSamples;
    float dFreq = (2.0f * 3.14156f * (acc ? ACCCLICKFREQ : CLICKFREQ)) / durInSamples;
    float freqAngle = 0.0f;
    for(float ampAngle = 0.0f; ampAngle < 3.14156f && ptr < endOfBuffer; ampAngle += dAngle) {
        float amp = 1.0f;//float(sin(double(ampAngle)));
        writeSample(ptr, amp * float(sin(double(freqAngle))));

        freqAngle += dFreq;
        ptr = ptrToNextSample(ptr);
    }
}

void *ClickGenerator::timeToPtr1M8(void *basePtr, float time) throw() {
    return(static_cast<char *>(basePtr) + static_cast<unsigned int>((time * 11025) + 0.5f));
}

float ClickGenerator::ptrToTime1M8(void *ptr) throw() {
    return(int(ptr) / 11025.0f);
}

void *ClickGenerator::ptrToNextSample1M8(void *ptr) throw() {
    return(static_cast<char *>(ptr) + 1);
}

void ClickGenerator::writeSample1M8(void *ptr, float value) throw() {
    *static_cast<char *>(ptr) = char(value * 127.0f);
}

void *ClickGenerator::timeToPtr2M8(void *basePtr, float time) throw() {
    return(static_cast<char *>(basePtr) + static_cast<unsigned int>((time * 22050) + 0.5f));
}

float ClickGenerator::ptrToTime2M8(void *ptr) throw() {
    return(int(ptr) / 22050.0f);
}

void *ClickGenerator::ptrToNextSample2M8(void *ptr) throw() {
    return(static_cast<char *>(ptr) + 1);
}

void ClickGenerator::writeSample2M8(void *ptr, float value) throw() {
    *static_cast<char *>(ptr) = char(value * 127.0f);
}

const float ClickGenerator::ACCCLICKFREQ = 440.0f;
const float ClickGenerator::CLICKFREQ = 220.0f;
const float ClickGenerator::CLICKDURARTION = 0.025f;
